package com.manage.platform.utils;

import com.manage.platform.vo.IndicatorSystemTaskVo;
import com.manage.platform.vo.TaskTreePartOneVo;
import com.manage.platform.vo.TaskTreePartVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 组织树形结构工具类
 */
public class TreeListTaskTreeUtil {

    /**
     * 构建树形结构
     * @param list 所有组织节点列表
     * @return 树形结构列表
     */
    public static List<TaskTreePartVo> buildMenuTreeList(List<TaskTreePartVo> list) {
        // 使用Map缓存提高查找效率
        Map<Long, TaskTreePartVo> nodeMap = list.stream()
                .collect(Collectors.toMap(TaskTreePartVo::getId, node -> node));

        List<TaskTreePartVo> rootNodes = new ArrayList<>();

        // 构建父子关系
        for (TaskTreePartVo node : list) {
            Long parentId = node.getParentId();
            if (parentId == null || parentId.equals(0L)) {
                rootNodes.add(node);
            } else {
                TaskTreePartVo parent = nodeMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    //处理第三级任务，


                    parent.getChildren().add(node);
                }
            }
        }
        return rootNodes;
    }


    /**
     * 构建树形结构
     * @param list 所有组织节点列表
     * @return 树形结构列表
     */
    public static List<TaskTreePartOneVo> buildMenuTreeListOne(List<TaskTreePartOneVo> list) {
        // 使用Map缓存提高查找效率
        Map<Long, TaskTreePartOneVo> nodeMap = list.stream()
                .collect(Collectors.toMap(TaskTreePartOneVo::getId, node -> node));

        List<TaskTreePartOneVo> rootNodes = new ArrayList<>();

        // 构建父子关系
        for (TaskTreePartOneVo node : list) {
            Long parentId = node.getParentId();
            if (parentId == null || parentId.equals(0L)) {
                rootNodes.add(node);
            } else {
                TaskTreePartOneVo parent = nodeMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(node);
                }
            }
        }
        return rootNodes;
    }



    /**
     * 构建树形结构
     * @param list 所有组织节点列表
     * @return 树形结构列表
     */
    /**
     *
     */
    public static List<IndicatorSystemTaskVo> buildMenuTreeListTwo(List<IndicatorSystemTaskVo> list) {
        try {
            // 使用Set去重
            Set<Long> seenIds = new HashSet<>();
            List<IndicatorSystemTaskVo> uniqueList = new ArrayList<>();

            for (IndicatorSystemTaskVo node : list) {
                if (seenIds.add(node.getId())) {
                    uniqueList.add(node);
                }
            }
            // 使用LinkedHashMap保持插入顺序
            Map<Long, IndicatorSystemTaskVo> nodeMap = new LinkedHashMap<>();
            for (IndicatorSystemTaskVo node : uniqueList) {
                nodeMap.put(node.getId(), node);
            }
            List<IndicatorSystemTaskVo> rootNodes = new ArrayList<>();

            for (IndicatorSystemTaskVo node : uniqueList) {
                Long parentId = node.getParentId();
                if (parentId == null || parentId.equals(0L)) {
                    rootNodes.add(node);
                } else {
                    IndicatorSystemTaskVo parent = nodeMap.get(parentId);
                    if (parent != null) {
                        if (parent.getChildren() == null) {
                            parent.setChildren(new ArrayList<>());
                        }
                        parent.getChildren().add(node);
                    }else {
                        rootNodes.add(node);
                    }
                }
            }
            return rootNodes;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }


    /**
     * 构建树形结构
     * @param list 所有组织节点列表
     * @return 树形结构列表
     */
    public  List<TaskTreePartVo> buildMenuTreeListThree(List<TaskTreePartVo> list) {
        // 使用Map缓存提高查找效率
        Map<Long, TaskTreePartVo> nodeMap = list.stream()
                .collect(Collectors.toMap(TaskTreePartVo::getId, node -> node));

        List<TaskTreePartVo> rootNodes = new ArrayList<>();

        // 构建父子关系
        for (TaskTreePartVo node : list) {
            Long parentId = node.getParentId();
            if (parentId == null || parentId.equals(0L)) {
                rootNodes.add(node);
            } else {
                TaskTreePartVo parent = nodeMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    //处理第三级任务，查询成果
                     if (node.getLevel() == 3) {

                     }

                    parent.getChildren().add(node);
                }
            }
        }
        return rootNodes;
    }

}
